(function () {
    window.Ball = class BallPre {
        constructor () {
            this.x = 100
            this.y = 100
            this.r = 50
            this.speed = 9
            this.angle = 60
            // cos和sin函数的参数是弧度
            this.dX = Math.cos(this.angle * Math.PI / 180) * this.speed
            this.dY = Math.sin(this.angle * Math.PI / 180) * this.speed
        }
        update () {
            this.x+=this.dX
            this.y+=this.dY
            // 判断碰边
            if (this.y >= game.canvas.height - this.r || this.y <= this.r) {
                this.angle = -this.angle
            }
            if (this.x + this.r >= game.canvas.width || this.x <= this.r) {
                this.angle = 180 - this.angle
            }
            this.dX = Math.cos(this.angle * Math.PI / 180) * this.speed
            this.dY = Math.sin(this.angle * Math.PI / 180) * this.speed
        }
        render () {
            game.context.beginPath()
            game.context.arc(this.x, this.y, this.r, 0, Math.PI / 180 * 360, false)
            game.context.fillStyle='pink'
            game.context.fill()
            game.context.closePath()
        }
    }
})()